import 'package:dailoz/dio/http_util.dart';
import 'package:dailoz/response_json_entity.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AnalyzePageViewModel extends ChangeNotifier {
  List<dynamic> _taskList = [];
  List<dynamic> _taskDayList = [];
  List<dynamic> _taskTotalTime = [];
  List<dynamic> _tagList = [];
  final List<bool> _lineIsShow = [true, true];
  final List<Color> _colors = [
    Colors.red,
    Colors.lightGreen,
    Colors.blue,
    Colors.orange,
    Colors.yellow,
  ];

  final List<Color> _gradientColors = [
    const Color(0xff23b6e6),
    const Color(0xff02d39a),
  ];

  bool _isCurved = false;
  bool _isLoading = false;

  int? _touchedIndex;
  int? _clickIndex = 0;
  int? _tagTouchedIndex;

  List<dynamic> get taskList {
    return _taskList;
  }

  List<bool> get lineIsShow {
    return _lineIsShow;
  }

  List<Color> get colors {
    return _colors;
  }

  List<dynamic> get tagList {
    return _tagList;
  }

  List<Color> get gradientColors {
    return _gradientColors;
  }

  List<dynamic> get taskDayList {
    return _taskDayList;
  }

  List<dynamic> get taskTotalTime {
    return _taskTotalTime;
  }

  bool get isLoading {
    return _isLoading;
  }

  bool get isCurved {
    return _isCurved;
  }

  int? get touchedIndex {
    return _touchedIndex;
  }

  int? get clickIndex {
    return _clickIndex;
  }

  int? get tagTouchedIndex {
    return _tagTouchedIndex;
  }

  void changeTaskDayList(newValue) {
    _taskDayList = newValue;
    notifyListeners();
  }

  void changeIsCurved(newValue) {
    _isCurved = newValue;
    notifyListeners();
  }

  void changeTouchedIndex(newValue) {
    _touchedIndex = newValue;
    notifyListeners();
  }

  void changeTagTouchedIndex(newValue) {
    _tagTouchedIndex = newValue;
    notifyListeners();
  }

  void changeLineIsShow(int index, newValue) {
    _lineIsShow[0] = newValue;
    notifyListeners();
  }

  void changeClickIndex(newValue) {
    _clickIndex = newValue;
    notifyListeners();
  }

  Future<void> fetchTypeAnalyze() async {
    _taskList.clear();
    _isLoading = true;
    final prefs = await SharedPreferences.getInstance();
    String? id = prefs.getString("user-id");
    try {
      Map<String, dynamic> response =
          await HttpUtil().get("/statistics", data: {"id": id});
      ResponseJsonEntity responseJsonEntity =
          ResponseJsonEntity.fromJson(response);
      if (responseJsonEntity.code == 200) {
        if (responseJsonEntity.data != null) {
          _taskList = responseJsonEntity.data;
        }
      }
    } catch (e) {
      print(e);
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }

  Future<void> fetchTypeDay(int day) async {
    _taskDayList.clear();
    _isLoading = true;
    final prefs = await SharedPreferences.getInstance();
    String? id = prefs.getString("user-id");
    try {
      var response = await HttpUtil()
          .get("/statistics/typeDay", data: {"id": id, "day": day});
      ResponseJsonEntity responseJsonEntity =
          ResponseJsonEntity.fromJson(response);
      if (responseJsonEntity.code == 200) {
        if (responseJsonEntity.data != null) {
          _taskDayList = responseJsonEntity.data;
        }
      }
    } catch (e) {
      print(e);
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }

  Future<void> fetchTypeSevenDay() async {
    _taskDayList.clear();
    _isLoading = true;
    final prefs = await SharedPreferences.getInstance();
    String? id = prefs.getString("user-id");
    try {
      Map<String, dynamic> response =
          await HttpUtil().get("/statistics/typeSevenDay", data: {"id": id});
      ResponseJsonEntity responseJsonEntity =
          ResponseJsonEntity.fromJson(response);
      if (responseJsonEntity.code == 200) {
        if (responseJsonEntity.data != null) {
          _taskDayList = responseJsonEntity.data;
        }
      }
    } catch (e) {
      print(e);
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }

  Future<void> fetchTaskTotalTimePerDay() async {
    _taskTotalTime.clear();
    _isLoading = true;
    final prefs = await SharedPreferences.getInstance();
    String? userId = prefs.getString("user-id");
    try {
      var response = await HttpUtil().get("/statistics/taskTotalTime",
          data: {"userId": userId, "time": DateTime.now()});
      ResponseJsonEntity responseJsonEntity =
          ResponseJsonEntity.fromJson(response);
      if (responseJsonEntity.code == 200) {
        if (responseJsonEntity.data != null) {
          _taskTotalTime = responseJsonEntity.data;
        }
      }
    } catch (e) {
      print(e);
    } finally {
      notifyListeners();
      _isLoading = false;
    }
  }

  Future<void> fetchTag() async {
    final prefs = await SharedPreferences.getInstance();
    String? userId = prefs.getString("user-id");
    try {
      Map<String, dynamic> response =
          await HttpUtil().get("/statistics/tag", data: {"userId": userId});
      ResponseJsonEntity responseJsonEntity =
          ResponseJsonEntity.fromJson(response);
      if (responseJsonEntity.code == 200) {
        if (responseJsonEntity.data != null) {
          _tagList = responseJsonEntity.data;
        }
      }
    } catch (e) {
      print(e);
    } finally {
      notifyListeners();
    }
  }
}
